﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Genuilder.Extensibility
{
	public enum SourceType
	{
		Compile,
		None,
		Page,
		ApplicationDefinition,
		EntityDeploy,
		EmbeddedResource
	}

	public class SourceMetadata
	{
		public string Name
		{
			get;
			set;
		}
		public string Value
		{
			get;
			internal set;
		}

		public SourceMetadata(string name)
		{
			this.Name = name;
		}
		public override string ToString()
		{
			return Name + " - " + Value;
		}
	}
	public class SourceMetadataCollection : IEnumerable<SourceMetadata>
	{
		List<SourceMetadata> _Metadatas = new List<SourceMetadata>();
		public bool? Visible
		{
			get
			{
				var visible = this["Visible"];
				if(visible == null)
					return null;
				return bool.Parse(visible);
			}
			//set
			//{
			//    if(value.HasValue)
			//        this["Visible"] = value.Value.ToString();
			//    else
			//        this["Visible"] = null;
			//}
		}

		public string FullPath
		{
			get
			{
				return this["FullPath"];
			}
		}
		public string RootDir
		{
			get
			{
				return this["RootDir"];
			}
		}
		public string Filename
		{
			get
			{
				return this["Filename"];

			}
		}
		public string Extension
		{
			get
			{
				return this["Extension"];
			}
		}
		public string RelativeDir
		{
			get
			{
				return this["RelativeDir"];
			}
		}
		public string Directory
		{
			get
			{
				return this["Directory"];

			}
		}
		public string RecursiveDir
		{
			get
			{
				return this["RecursiveDir"];

			}
		}
		public string Identity
		{
			get
			{
				return this["Identity"];

			}
		}
		

		public string this[string name]
		{
			get
			{
				var metadata = GetMetadata(name);
				if(metadata == null)
					return null;
				return metadata.Value;
			}
			internal set
			{
				var metadata = GetMetadata(name);
				if(metadata == null)
				{
					metadata = new SourceMetadata(name);
					_Metadatas.Add(metadata);
				}
				if(value == null)
				{
					_Metadatas.Remove(metadata);
				}
				metadata.Value = value;
			}
		}

		private SourceMetadata GetMetadata(string name)
		{
			return _Metadatas.FirstOrDefault(m => m.Name == name);
		}
		#region IEnumerable<SourceMetadata> Members

		public IEnumerator<SourceMetadata> GetEnumerator()
		{
			return _Metadatas.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion
	}

	public class SourceFile
	{
		public SourceFile(string name, Extensibility.SourceType sourceType, SourceMetadataCollection metadata = null)
		{
			if(metadata == null)
				metadata = new SourceMetadataCollection();
			if(name == null)
				throw new ArgumentNullException("name");
			_MetadataCollection = metadata;
			_Name = name;
			_SourceType = sourceType;
			_hashCode = Tuple.Create(Name, SourceType).GetHashCode();
		}

		int _hashCode;

		private readonly SourceMetadataCollection _MetadataCollection;
		public SourceMetadataCollection MetadataCollection
		{
			get
			{
				return _MetadataCollection;
			}
		}

		private readonly SourceType _SourceType;
		public SourceType SourceType
		{
			get
			{
				return _SourceType;
			}
		}
		private readonly string _Name;
		public string Name
		{
			get
			{
				return _Name;
			}
		}


		public override string ToString()
		{
			return SourceType.ToString() + " " + Name;
		}

		public override bool Equals(object obj)
		{
			SourceFile item = obj as SourceFile;
			if(item == null)
				return false;
			return _hashCode.Equals(item._hashCode);
		}

		public override int GetHashCode()
		{
			return _hashCode;
		}
	}
}
